import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import { DeviceProvider } from '../../providers/service/device'
import { PageModel } from '../../models/page'
import { Keyboard } from '@ionic-native/keyboard';
import { AlarmDetailPage } from './alarm-detail/alarm-detail';

/**
 * Generated class for the AlarmPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
  selector: 'page-alarm',
  templateUrl: 'alarm.html',
  providers: [Keyboard]
})
export class AlarmPage {

  constructor(public navCtrl: NavController,
    public deviceProvider: DeviceProvider,
    public keyboard: Keyboard,
    public navParams: NavParams) {
  }

  

  isReady = false;
  isLoading = true;
  pageObj = new PageModel();
  //searchKey = '';
  list = []

  ionViewDidLoad() {
    // console.log('ionViewDidLoad DevicePage');
    this.isReady = true;
    this.getDataList(true);
    // pageSize    : 10,
    // pageNum     : 1,
    // total       : 1,
    // timeRange   : '',    //时间范围
    // physicalCode: '', //序列号
    // status      : ''     //设备状态

  }

  /**
   * 下拉刷新
   * @param refresher
   */
  doRefresh(refresher) {
    this.getDataList(true).then(() => {
      refresher.complete();
    })
  }

  /**
   * 上拉加载
   * @param infiniteScroll
   */
  doInfinite(infiniteScroll) {
    this.getDataList().then(() => {
      infiniteScroll.complete();
    })
  }

  // //搜索
  // searchOnHandler() {
  //   this.getDataList(true);
  // }

  // //搜索取消
  // searchOnCancel(event) {
  //   event.stopPropagation();
  //   this.searchOnHandler();
  //   setTimeout(function () {
  //     console.log(this.keyboard)
  //     try {
  //       this.keyboard || this.keyboard.close(); //关闭keyboard
  //     } catch (e) { }
  //     event.target.blur();
  //   }, 500);
  // }

  getDataList(isRefresh = false) {
    this.isLoading = true;
    if (isRefresh) {
      this.pageObj.pageNum = 1;
      this.list = [];
    }

    let elseFilter = {
      //physicalCode: this.searchKey
    }

    return this.deviceProvider.getAlarmList({ ...this.pageObj, ...elseFilter }).then((res: any) => {
      console.log(res)
      this.pageObj.pageSize = res.pageSize;
      this.pageObj.pageNum = res.pageNum;
      this.pageObj.total = res.total;
      this.pageObj.isLastPage = res.isLastPage;
      this.pageObj.pageNum++;
      this.isLoading = false;
      if(!res.list) {
        res.list = [];
      }
      this.list = this.list.concat(res.list);
  
    }, err => {
      this.isLoading = false;
    });

  }

  pToDetail(item){
    this.navCtrl.push(AlarmDetailPage, {physicalCode: item.physicalCode});
  }

}
